const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            dataForm1:{},
            dataForm2:{},
            childDataForm1:[],
            policy: [],
            land:[],
            record:[],
            Year: new Date().getFullYear(),
            crop:[],
            policy1:[],
            Ownership_Rights:{
                10:'国有土地所有权',
                20:'国有土地使用权',
                30:'集体土地所有权'
            },
            village:[],
            cityValue:[],
            Natural_Resource_Usage:[],
            natural_resources_quality_grade:[],
            tableData:[],
            personal_information:[]
        };
    },
    mounted() {
        this.getcrop();
        this.getCategory();
        if (this.action == 'Add') {
            this.changeYear(this.Year);
        }
       
    },
    watch:{
        collapse(val){
            console.log(val);
        },
        'dataForm2.Natural_Resource_Usage_SerialNumber'(val){
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_usage',
                    Condition: "Row_Status = '0'"
                },
                success: (data) => {
                    let obj = data.filter(i=>i.Natural_Resource_Usage_SerialNumber == val)[0];
                    let obj1 = data.filter(i=>i.Natural_Resource_Usage_SerialNumber == obj.Parent_SerialNumber)[0];
                    this.cityValue = [obj1.Natural_Resource_Usage_SerialNumber,val]
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        }
    },
    methods: {
        handleEdit(row){
            window.location.href = './add2.html?action=Add&nodeKey=' + row.Application_Distribution_SerialNumber + "&Application_Amount=" + row.Application_Amount + "&Application_Amount1=" + row.Application_Amount1
        },
        getCategory() {
            if (this.action=="Modify") {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableName`,
                    type: "POST",
                    data: {
                        Data: 'subsidy_policy',
                        Condition: "Row_Status='0'",
                    },
                    success: (data) => {
                        this.policy = data; // 直接使用 this
                    }, complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            };
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.personal_information = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.category = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "POST",
                data: {
                    SQLSerialNumber: 't_subsidy_application_distribution',
                    Condition: window.btoa(unescape(encodeURIComponent(this.nodeKey))),
                },
                success: (data) => {
                    console.log(data);
                    this.tableData = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName:'natural_resources_usage',
                    NodeCode:'',
                },
                success: (data) => {
                    data.forEach(item=>{
                        if (item.children) {
                            item.children.forEach(it=>{
                                it.Natural_Resource_Usage_Name = it.Natural_Resource_Usage_Name + (it.Natural_Resource_Type? ' - ' + it.Natural_Resource_Type : '')
                            })
                        }
                    })
                    this.Natural_Resource_Usage = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_usage',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Natural_Resource_Usage1 = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_tags',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Natural_Resource_Tag = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_quality_grade',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.natural_resources_quality_grade = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'base_system_village',
                    Condition: "TownNumber LIKE '232723%'",
                },
                success: (data) => {
                    this.village = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        getcrop(){
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'crop',
                    Condition: `Row_Status='0'`,
                },
                success: (res) => {
                    this.crop = res;
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        changeYear(val){
            $.ajax({headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'subsidy_policy',
                    Condition: "Row_Status='0' AND Year(Effective_Date)='"+val+"'",
                },
                success: (data) => {
                    this.policy = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        getData(nodeKey) {//查询
            const that = this;
            //获取对应子表表单对象
            let lJSONObject_Children = new Array();
            let lJSONObject_ChildData = new Object();
            try {
                //可能涉及多个子表
                document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                    let lJSONObject_ChildData_Options = new Object();
                    lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                    let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                    let ls_ChildDataForm = lJSONObject_ChildData_Options.form;

                    lJSONObject_ChildData[ls_ChildTableName] = ls_ChildDataForm;
                    lJSONObject_Children[ls_ChildTableName] = 'ChildTable';
                })
            } catch (error) { }
            //获得对应表单对象
            let lJSONObject_Data_Options = new Object();
            lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));
            let ls_DataForm = lJSONObject_Data_Options.form;
            let ls_TableName = lJSONObject_Data_Options.TableName;
            let ls_primary_key = lJSONObject_Data_Options.primary_key

            let lJSONObject_Data = new Object();
            lJSONObject_Data[ls_TableName] = ls_DataForm;
            lJSONObject_Data = { ...lJSONObject_Data, ...lJSONObject_ChildData };

            let lJSONObject_TableName = new Object();
            lJSONObject_TableName[ls_TableName] = 'MainTable'
            lJSONObject_TableName = { ...lJSONObject_TableName, ...lJSONObject_ChildData };
            for (let key in lJSONObject_TableName) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: key,
                        Condition: `${ls_primary_key}='${nodeKey}'`,
                    },
                    success: (res) => {
                        if (lJSONObject_TableName[key] == 'MainTable') {
                            that[lJSONObject_Data[key]] = res[0];
                            if (this.action=='Modify') {
                                this.changePolicy(this.dataForm.Policy_SerialNumber);
                                this.changeLand(this.dataForm.Land_SerialNumber)
                            }  
                        } else {
                            that[lJSONObject_Data[key]] = res;
                        }
                    }, complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                });
            }
        },
        changePolicy(val){
            
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'subsidy_policy',
                    Condition: `Policy_SerialNumber='${val}'`,
                },
                success: (res) => {
                    this.dataForm1 = res[0]
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'subsidy_policy_detail',
                    Condition: `Policy_SerialNumber='${val}'`,
                },
                success: (res) => {
                    this.childDataForm = res
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
            $.ajax({headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 'f_land_crop_natural_resources',
                    Condition: window.btoa(unescape(encodeURIComponent(val))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    this.land = data;
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            })
        },
        changeLand(val){
            $.ajax({headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "POST",
                data: {
                    SQLSerialNumber: "f_subsidy_policy_detail",
                    Condition: window.btoa(unescape(encodeURIComponent(val+","+this.dataForm.Policy_SerialNumber))),
                },
                success: (data) => {
                    this.record = data;
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_management',
                    Condition: "Natural_Resource_SerialNumber='"+val+"'",
                },
                success: (data) => {
                    this.dataForm2 = data[0]; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "POST",
                data: {
                    SQLSerialNumber: 't_subsidy_application',
                    Condition: window.btoa(unescape(encodeURIComponent("%%,%%"))),
                    ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND subsidy_application.Land_SerialNumber='"+val+"'"))),
                },
                success: (data) => {
                    this.childDataForm1 = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus,{locale: zhCn});
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-select',ylSelect)
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
